// Sample code to create colour picker
/*
var pickerElem = "#PBCP1";
var swatchElem = "#swatch";
var objColourPicker = new PBcolourPicker(pickerElem, swatchElem);

$(function() {
	$(pickerElem).slider({
		orientation: 'horizontal',
		range: "min",
		max: 200,
		value: 0,
		slide: objColourPicker.alterDarkness,
		change: objColourPicker.alterDarkness,
		stop: function(){ alert(objColourPicker.getOutputColour()) }
	});
	
});
*/

function PBcolourPicker(bindingElement, swatchElement){

	this.swatchColour = 'rgb(0, 0, 0)';
	this.outputColour;
	this.sliderControl = bindingElement;
	this.swatchControl = swatchElement;
	var objectRef = this;

	this.updateSwatch = function(e){
		newColour = $(e).css('background-color');
		objectRef.swatchColour = newColour;
		$(objectRef.swatchControl + ', ' + objectRef.sliderControl).animate({'background-color': objectRef.swatchColour}, 200);
		
		c = objectRef.getRGB(newColour);
		if(c[0] == 128 && c[1] == 128 && c[2] == 128){
			// Black. Restrict to 100.
			$(objectRef.sliderControl).slider('option', 'max', 100);
		} else {
			$(objectRef.sliderControl).slider('option', 'max', 200);
		}
		objectRef.slideTo( $(objectRef.sliderControl).slider('option', 'max') / 2 );
		
		$(objectRef.sliderControl).children('a')[0].focus();
	}
	
	this.alterDarkness = function(event, ui){
		currentVals = objectRef.getRGB(objectRef.swatchColour);
		if( (currentVals[0] == 0 && currentVals[1] == 0 && currentVals[2] == 0) || (currentVals[0] == 128 && currentVals[1] == 128 && currentVals[2] == 128)){
			$(objectRef.sliderControl).slider('option', 'max', 100);
		} else {
			$(objectRef.sliderControl).slider('option', 'max', 200);
		}
		
		HSV = objectRef.RGBToHSV(currentVals[0], currentVals[1], currentVals[2]);
		
		if(ui.value > 100){
			HSV[1] = (100 - (ui.value - 100)) / 100;
		} else {
			HSV[2] = (ui.value * 0.01);
		}
		
		RGB = objectRef.HSVToRGB(HSV[0], HSV[1], HSV[2]);
		
		$(objectRef.swatchControl).css('background-color', 'rgb(' + RGB.join() + ')');
		
		hex = [];
		hex[0] = RGB[0].toString(16);
		hex[1] = RGB[1].toString(16);
		hex[2] = RGB[2].toString(16);
		
		for(c in hex) if(hex[c].length == 1) hex[c] = '0'+hex[c];
		hex = hex.join('');

		objectRef.outputColour = "#" + hex;
	}
	
	this.slideTo = function(val){
		$(objectRef.sliderControl).slider('value', val);
	}
	
	this.getOutputColour = function(){
		return objectRef.outputColour;
	}
	
	this.getRGB = function(i){
		if(i == null) return false;
		rgb = i.match(/^rgb\((\d+),\s*(\d+),\s*(\d+)\)$/);
		if(rgb == null) {
			// May be hex.
			hexVals = i.match(/^#([0-9A-Za-z]{2})([0-9A-Za-z]{2})([0-9A-Za-z]{2})$/);
			if(hexVals == null){
				return false;
			} else {
				hexVals = ['', parseInt(hexVals[1], 16), parseInt(hexVals[2], 16), parseInt(hexVals[3], 16)];
				for(i in hexVals){ if(hexVals[i].length == 1) hexVals[i] = '0'+hexVals[i] }
				
				rgb = ['', hexVals[1], hexVals[2], hexVals[3]];
			}
		}
		return [rgb[1], rgb[2], rgb[3]];			
	}

	this.RGBToHSV = function(r, g, b){
	    r = r/255, g = g/255, b = b/255;
	    var max = Math.max(r, g, b), min = Math.min(r, g, b);
	    var h, s, v = max;
	
	    var d = max - min;
	    s = max == 0 ? 0 : d / max;
	
	    if(max == min){
	        h = 0;
	    }else{
	        switch(max){
	            case r: h = (g - b) / d + (g < b ? 6 : 0); break;
	            case g: h = (b - r) / d + 2; break;
	            case b: h = (r - g) / d + 4; break;
	        }
	        h /= 6;
	    }
	
	    return [h.toFixed(2), s.toFixed(2), v.toFixed(2)];
	}		
	
	this.HSVToRGB = function(h, s, v){
	    var r, g, b;
	
	    var i = Math.floor(h * 6);
	    var f = h * 6 - i;
	    var p = v * (1 - s);
	    var q = v * (1 - f * s);
	    var t = v * (1 - (1 - f) * s);
	    
	    switch(i % 6){
	        case 0: r = v, g = t, b = p; break;
	        case 1: r = q, g = v, b = p; break;
	        case 2: r = p, g = v, b = t; break;
	        case 3: r = p, g = q, b = v; break;
	        case 4: r = t, g = p, b = v; break;
	        case 5: r = v, g = p, b = q; break;
	    }
	
	    return [Math.round(r * 255), Math.round(g * 255), Math.round(b * 255)];
	}	

};